/*
 * global.h
 *
 *  Created on: 13.10.2012
 *      Author: md5sum@alexsp.ru
 */

#ifndef GLOBAL_H_
#define GLOBAL_H_

#include <avr/io.h>
//#include <math.h>
#define USE_DEBUG
#include "usart.h"
#include <stdlib.h>

/*Подсветка*/
#define LCD_LAMP_pin (1<<0)
#define LCD_LAMP_port PORTA
#define LCD_LAMP_ddr DDRA

/*Кнопки*/
#define KEY_PIN PINC
#define KEY_PORT PORTC
#define KEY_DDR DDRC
#define K_LEFT 0x80
#define K_RIGHT 0x40
#define K_ENTER 0x20
#define K_EXIT 0x10

/*Ключи*/
#define OUT_PORT PORTD
#define OUT_DDR DDRD
#define OUT1 PD4
#define OUT2 PD5
#define OUT3 PD6
#define OUT4 PD7
#define HEATER_ON() PORTD|=(1<<OUT1)
#define HEATER_OFF() PORTD&=~(1<<OUT1)
#define PUMP_ON() PORTD|=(1<<OUT2)
#define PUMP_OFF() PORTD&=~(1<<OUT2)
#define OUT_ALL_OFF() OUT_PORT&=~((1<<OUT1)|(1<<OUT2)|(1<<OUT3)|(1<<OUT4))


/*Коды ошибок оборудования: термометров и часов.*/
typedef enum{SUCCESS=0,ERROR,CRC,SHORT,OPEN,CLOCK_STOP,PWR_FAIL} hw_ret_status;
#define USART_BAUD 38400
//#define DBG_M41 1

/*расположение датчиков ds1820 указываем DDRx !!!!*/
#define DS1820_1_port DDRB
#define DS1820_1_bit (1<<PB2)
#define DS1820_2_port DDRB
#define DS1820_2_bit (1<<PB3)
#define DS1820_3_port DDRB
#define DS1820_3_bit (1<<PB4)

#define DS1820_1 (uint8_t *) &DS1820_1_port , DS1820_1_bit
#define DS1820_2 (uint8_t *) &DS1820_2_port , DS1820_2_bit
#define DS1820_3 (uint8_t *) &DS1820_3_port , DS1820_3_bit

#define ROOM DS1820_1
#define HEATER DS1820_2
#define RADIATOR DS1820_3

#define MAX_DS_ERROR 9

typedef struct{
	uint8_t sec;
	uint8_t min;
	uint8_t hour;
	uint8_t day_w;
	uint8_t day;
	uint8_t month;
	uint8_t year;

	union {
		uint32_t unix_32;
		struct {uint8_t b0,b1,b2,b3;} unix_8;
	};
	uint8_t ten_m;
} tm;


extern int16_t room_temp; //температура помещения
extern int16_t heater_temp; //температура нагревателя
extern int16_t radiator_temp; //температура батареи
extern int16_t heater_temp_max; // максимальная температура для нагревателя ~80-90 град
extern int16_t heater_temp_min; // минимальная температура для нагревателя при которой опять включаем нагрев
extern int16_t radiator_temp_offset; //температура отключения насоса - относительно температуры помещения
extern int16_t error_temp;
extern hw_ret_status room_last_err,heater_last_err,radiator_last_err;
extern uint8_t room_err_cnt;
extern uint8_t heater_err_cnt;
extern uint8_t radiator_err_cnt;




extern tm current_time;
extern tm last_pow_off_time;
extern tm tmp_time;
extern uint16_t user_time;
extern uint8_t m41_control;
//extern uint8_t m41_stop;
//extern uint8_t m41_halt;
extern uint8_t TWI_last_error;

extern uint8_t flags;
#define FLG_CLK_STOP (1<<7)
#define FLG_PWR_ERR (1<<6)
#define FLG_CONG_ERR (1<<5)
#define FLG_STOP_MAIN (1<<4)


extern uint8_t next_time;
extern int8_t current_temp;
extern int8_t next_temp;

typedef struct{
	uint8_t time;
	int8_t	termo;
}times_array_elem_t;
#define POINTS_ON_DAY 10
extern times_array_elem_t times_array[7][POINTS_ON_DAY];

/*
typedef struct {
	uint8_t heat:1;
	uint8_t cool:1;
	uint8_t major_err:1;
	uint8_t minor_err:1;
	uint8_t user_set:1;

}main_status_t;
*/
typedef uint8_t main_status_t;
extern main_status_t main_status;
#define STATUS_WAIT 0
#define STATUS_HEAT (1<<0)
#define STATUS_COOL (1<<1)
#define STATUS_DS_ERR (1<<2)
#define STATUS_HEAT_ERR (1<<3)
#define STATUS_CLOCK_ERR (1<<4)
#define STATUS_USER_T (1<<5)
#define STATUS_POW_LOSS (1<<6)


#endif /* GLOBAL_H_ */
